---
title: 'Section 5: Candidate-Choice Experiments'
author: "Milan Svolik"
date: "4/20/2023"
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
rm(list = ls(all = TRUE))
library(tidyverse)
library(stargazer)
library(lmtest)
library(reshape2)
library(foreign)
library(ggplot2)
library(broom)
library(estimatr)
library(texreg)


set.seed(5877)
```

# Load the experiment data
```{R}
rm(list = ls(all = TRUE))
load("data/df_experiments.RData")
```

# Figure 5
* CANDIDATE-CHOICE EXPERIMENTS, 2019: THE “DEMOCRACY AROUND THE WORLD” BATTERY

### Prepare the textual description
```{R}
AWB_criticize_text <- c("Opposition candidates are banned from criticizing the government")
AWB_districts_text <- c("The largest party redrew districts to improve its chances of winning an election")
AWB_electronic_text <- c("Voting is conducted using electronic voting machines")
AWB_fired_text <- c("The government fired state employees who expressed support for an opposition party")
AWB_funding_text <- c("The government cut funding in districts that supported the opposition in the last election")
AWB_journalists_text <- c("Journalists frequently disagree with the president’s policies")
AWB_legislature_text <- c("The country’s president closed down the legislature and is governing by executive order")
AWB_media_text <- c("State media only report favorably about the governing political party")
AWB_military_text <- c("The military overthrew a government that engaged in corruption")
AWB_presidential_text <- c("The country changed its system from parliamentary to presidential")
AWB_protests_text <- c("Opposition parties organize protests against the government")
AWB_reelection_text <- c("The legislature passed a law that bans sitting presidents from running for re-election")
AWB_referenda_text <- c("Major political questions are decided by public referenda")
AWB_resources_text <- c("Candidates from the incumbent party use government resources when campaigning for re-election")
AWB_ruling_text <- c("The president refused to implement a ruling by the country’s highest court")
AWB_stepdown_text <- c("The incumbent president refused to step down from office after a narrow election defeat")
AWB_turnouAWB_text <- c("About a half of the country’s voters do not turn out to vote in legislative elections")
AWB_welfare_text <- c("A candidate in a presidential election promised an increase in welfare benefits in order to attract voters")

AWB_text <- c(AWB_criticize_text, AWB_districts_text, AWB_electronic_text, AWB_fired_text, AWB_funding_text, AWB_journalists_text, AWB_legislature_text, AWB_media_text, AWB_military_text, AWB_presidential_text, AWB_protests_text, AWB_reelection_text, AWB_referenda_text, AWB_resources_text, AWB_ruling_text, AWB_stepdown_text, AWB_turnouAWB_text,  AWB_welfare_text)
```

### Estimate the means and CIs 
```{R}
df_AWB <- df_experiments %>%
  select(rid, weight, starts_with("AWB_")) %>%
  pivot_longer(cols = starts_with("AWB_"), names_to = "item", values_to = "rating")

ols_AWB <- df_AWB %>%
  group_by(item) %>%
  do(lm_robust(rating ~ 1, data = ., clusters=rid, weight=weight) %>% tidy)

ols_AWB
```

### The means and CIs for the items reported in the text
```{R}
ols_AWB %>%
  filter(item %in% c("AWB_fired", "AWB_ruling", "AWB_stepdown")) %>%
  select(item, estimate, conf.low, conf.high) %>%
  mutate(across(estimate:conf.high, round, 2)) 
```

### Plot
```{R}
plot_ols_AWB  <- ols_AWB %>% 
  ungroup %>%
  mutate(term = AWB_text) %>%
  arrange(estimate) %>%
  select(term, estimate, std.error)

library(dotwhisker)
plot_ols_AWB  %>% 
  dwplot + 
  theme_bw() + 
  theme(legend.position="none") +
  xlab("Mean rating (95% confidence interval)") + 
  scale_x_continuous(limits=c(0,10)) 

# SAVE THE PLOT
ggsave("figures/figure_5.png", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_5.eps", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_5.pdf", dpi = 1200, width = 10, height = 6, units = "in")
```

# Table 4
* CANDIDATE-CHOICE EXPERIMENTS, 2019: THE DEMOCRATIC CHECK
* Candidate 1 is always AKP, Candidate 2 is either CHP or IYI
* Outcomes: 
  - vote_1: vote AKP
  - vote_2: vote CHP
  - vote_0: abstain
* Treatment: the AKP candidate endorses a position that violates democratic principles
  - treat: 1 if treatment; 0 otherwise

### Calculate the share of subjects in each outcome and treatment condition
```{R}
df_experiments %>%
  select(rid, district_id, weight, treat, vote_1, vote_2, vote_0) %>%
  filter(!is.na(treat)) %>%
  mutate(vote_share = vote_1/(vote_1 + vote_2)) %>%
  pivot_longer(cols = starts_with("vote_"), names_to = "vote", values_to = "choice") %>%
  group_by(treat, vote) %>% 
  do(lm_robust(choice ~ 1, data = ., clusters=district_id, weight=weight) %>% tidy) %>%
  arrange(vote, treat) %>%
  select(treat, vote, estimate, conf.low, conf.high) %>%
  mutate(across(estimate:conf.high, ~ .x*100)) %>%
  mutate(across(estimate:conf.high, ~ round(.x, 2)))
```
### Calculate the difference in each outcome between the two treatment conditions 
```{R}
df_experiments %>%
  select(rid, district_id, weight, treat, vote_1, vote_2, vote_0) %>%
  filter(!is.na(treat)) %>%
  mutate(vote_share = vote_1/(vote_1 + vote_2)) %>%
  pivot_longer(cols = starts_with("vote_"), names_to = "vote", values_to = "choice") %>%
  group_by(vote) %>% 
  do(lm_robust(choice ~ 1 + treat, data = ., clusters=district_id, weight=weight) %>% tidy) %>%
  arrange(vote) %>%
  select(vote, term, estimate, conf.low, conf.high) %>%
  filter(term=="treat") %>%
  mutate(across(estimate:conf.high, ~ .x*100)) %>%
  mutate(across(estimate:conf.high, ~ round(.x, 2)))
```

# Figure 6
* CANDIDATE-CHOICE EXPERIMENTS, 2019: VOTE FOR THE AKP, CHP, AND ABSTENTION BY THE DIFFERENCE IN RESPONDENTS’ APPROVAL OF PRESIDENT ERDOĞAN (AKP) AND THE CHP’S MAYORAL CANDIDATE EKREM İMAMOĞLU
```{R}
# CREATE THE 5 SUBGROUPS FOR THE ERDOGAN-IMAMOGLU APPROVAL DIFFERENTIAL
approval_diff <- df_experiments$approval_erdogan - df_experiments$approval_imamoglu
approval_diff_5 <- cut(approval_diff, breaks = c(-11, -7, -3, 2, 6, 10))

#Calculate the share of subjects in each outcome and treatment condition
df_plot <- df_experiments %>%
  mutate(approval_diff_5) %>%
  filter(!is.na(approval_diff_5)) %>%
  select(rid, district_id, weight, treat, vote_1, vote_2, vote_0, approval_diff_5) %>%
  filter(!is.na(treat)) %>%
  pivot_longer(cols = starts_with("vote_"), names_to = "vote", values_to = "choice") %>%
  group_by(approval_diff_5, treat, vote) %>% 
  do(lm_robust(choice ~ 1, data = ., clusters=district_id, weight=weight) %>% tidy) %>%
  arrange(vote, treat, approval_diff_5) %>%
  select(treat, vote, approval_diff_5, estimate, conf.low, conf.high)
```
### PLOT THE AKP VOTE
```{r}
plot_legend_labels <- c("     AKP v. CHP","D- AKP v. CHP")
plot_x_label <- c("Difference in approval: Erdo\u011fan (AKP) - \u0130mamo\u011flu (CHP)")
plot_x_labels <- c("[-10, -7]", "[-6, -3]", "[-2, 2]", "[3,6]", "[7, 10]")

df_plot %>% 
  filter(vote=="vote_1") %>%
  mutate(treat=as.factor(treat)) %>%
  ggplot(aes(x=approval_diff_5, y=estimate, col=treat)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  theme_minimal() +
  geom_line(aes(col=treat, group=treat, linetype = treat),  size=1) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, col=treat, group=treat), width=0.1) +
  geom_point(aes(col=treat, shape=treat), size=2) +
  scale_shape_manual(name  ="", values=c(17, 16), labels=plot_legend_labels) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=plot_legend_labels) +
  scale_color_manual(name  ="", values=c("black", "blue"), labels=plot_legend_labels) +
  scale_x_discrete(labels=plot_x_labels) + 
  labs(
       x = plot_x_label, y = "Fraction",
       title = "Vote AKP"
    ) 

# SAVE THE PLOT
ggsave("figures/figure_6_AKP.png", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_6_AKP.eps", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_6_AKP.pdf", dpi = 1200, width = 10, height = 6, units = "in")
```
### PLOT THE CHP VOTE
```{r}
df_plot %>% 
  filter(vote=="vote_2") %>%
  mutate(treat=as.factor(treat)) %>%
  ggplot(aes(x=approval_diff_5, y=estimate, col=treat)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  theme_minimal() +
  geom_line(aes(col=treat, group=treat, linetype = treat),  size=1) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, col=treat, group=treat), width=0.1) +
  geom_point(aes(col=treat, shape=treat), size=2) +
  scale_shape_manual(name  ="", values=c(17, 16), labels=plot_legend_labels) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=plot_legend_labels) +
  scale_color_manual(name  ="", values=c("black", "blue"), labels=plot_legend_labels) +
  scale_x_discrete(labels=plot_x_labels) + 
  labs(
       x = plot_x_label, y = "Fraction",
       title = "Vote CHP"
    ) 

# SAVE THE PLOT
ggsave("figures/figure_6_CHP.png", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_6_CHP.eps", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_6_CHP.pdf", dpi = 1200, width = 10, height = 6, units = "in")
```
### PLOT ABSTENTIONS
```{r}
df_plot %>% 
  filter(vote=="vote_0") %>%
  mutate(treat=as.factor(treat)) %>%
  ggplot(aes(x=approval_diff_5, y=estimate, col=treat)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  theme_minimal() +
  geom_line(aes(col=treat, group=treat, linetype = treat),  size=1) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, col=treat, group=treat), width=0.1) +
  geom_point(aes(col=treat, shape=treat), size=2) +
  scale_shape_manual(name  ="", values=c(17, 16), labels=plot_legend_labels) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=plot_legend_labels) +
  scale_color_manual(name  ="", values=c("black", "blue"), labels=plot_legend_labels) +
  scale_x_discrete(labels=plot_x_labels) + 
  labs(
       x = plot_x_label, y = "Fraction",
       title = "Abstain"
    ) 

# SAVE THE PLOT
ggsave("figures/figure_6_ABS.png", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_6_ABS.eps", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_6_ABS.pdf", dpi = 1200, width = 10, height = 6, units = "in")
```
# Table 5 
* CANDIDATE-CHOICE EXPERIMENTS, 2019: HETEROGENEITY IN PUNISHMENT BY RESPONDENT COVARIATES

### Prepare the data for estimation
```{R}
df_het <- df_experiments %>% 
  select(rid:weight, district_id, treat:vote_0, age_cat:interest) %>%
  mutate(
    AKP_share = vote_1/(vote_1 + vote_2)
    )
```

### HETEROGENEITY: THE AKP VOTE
```{R}
df_het_AKP <- df_het %>%
  select(rid, weight, district_id, treat, vote_1, age_cat:interest)
ols_het_AKP <- lm_robust(vote_1 ~ treat*(.-rid-weight-district_id), data=df_het_AKP, weights = weight, clusters=district_id)
```

### HETEROGENEITY: THE CHP VOTE
```{R}
df_het_CHP <- df_het %>%
  select(rid, weight, district_id, treat, vote_2, age_cat:interest)
ols_het_CHP <- lm_robust(vote_2 ~ treat*(.-rid-weight-district_id), data=df_het_CHP, weights = weight, clusters=district_id)
```

### HETEROGENEITY: ABSTENTIONS
```{R}
df_het_ABS <- df_het %>%
  select(rid, weight, district_id, treat, vote_0, age_cat:interest)
ols_het_ABS <- lm_robust(vote_0 ~ treat*(.-rid-weight-district_id), data=df_het_ABS, weights = weight, clusters=district_id)
```

### HETEROGENEITY: AKP's TWO-PARTY VOTE SHARE
```{R}
df_het_AKP_share <- df_het %>%
  select(rid, weight, district_id, treat, AKP_share, age_cat:interest)
ols_het_AKP_share <- lm_robust(AKP_share ~ treat*(.-rid-weight-district_id), data=df_het_AKP_share, weights = weight, clusters=district_id)
```

## CONSTRUCT THE TABLE
```{R, eval=FALSE}
library(modelsummary)

# AKP VOTE
est_ols_het_1 <- ols_het_AKP
est_ols_het_1_out <- tidy(est_ols_het_1)

est_ols_het_1_out_alphas <- est_ols_het_1_out %>%
  slice(c(1:2))

est_ols_het_1_out_betas <- est_ols_het_1_out %>%
  filter(!grepl("treat:",term)) %>% 
  slice(-c(1:2))

est_ols_het_1_out_gammas <- est_ols_het_1_out %>%
  filter(grepl("treat:",term)) 

table_coefs_alphas <- est_ols_het_1_out_alphas
table_coefs_alphas$term <- c("Intercept", "$D^-$")
table_coefs_betas <- est_ols_het_1_out_betas
table_coefs_betas$term <- c("Age: 31-40","Age: 41-50", "Age: 51+", "Sex: Male", "Education: High school", "Education: College or higher", "Unemployed", "Student", "State employed", "Wealth: Lower middle", "Wealth: Upper middle", "Wealth: High", "Pray: Monthly, Holidays", "Pray: Weekly", "Pray: Everyday", "Kurdish", "Urban", "Financially: Worse", "Financially: Same", "Financially: Better", "Financially: Much better", "Interested in politics: Little", "Interested in politics: Some", "Interested in politics: A lot", "Interested in politics: Very much")  
table_coefs_gammas <- est_ols_het_1_out_gammas
table_coefs_gammas$term <- c("Age: 31-40","Age: 41-50", "Age: 51+", "Sex: Male", "Education: High school", "Education: College or higher", "Unemployed", "Student", "State employed", "Wealth: Lower middle", "Wealth: Upper middle", "Wealth: High", "Pray: Monthly, Holidays", "Pray: Weekly", "Pray: Everyday", "Kurdish", "Urban", "Financially: Worse", "Financially: Same", "Financially: Better", "Financially: Much better", "Interested in politics: Little", "Interested in politics: Some", "Interested in politics: A lot", "Interested in politics: Very much")  

table_stats <- glance(est_ols_het_1)[1,c(6,1)]

table_out_alphas_1 <- list(
  tidy = table_coefs_alphas,
  glance = table_stats)
class(table_out_alphas_1) <- "modelsummary_list"

table_out_betas_1 <- list(
  tidy = table_coefs_betas,
  glance = table_stats)
class(table_out_betas_1) <- "modelsummary_list"

table_out_gammas_1 <- list(
  tidy = table_coefs_gammas,
  glance = table_stats)
class(table_out_gammas_1) <- "modelsummary_list"

# CHP VOTE
est_ols_het_2 <- ols_het_CHP
est_ols_het_2_out <- tidy(est_ols_het_2)

est_ols_het_2_out_alphas <- est_ols_het_2_out %>%
  slice(c(1:2))

est_ols_het_2_out_betas <- est_ols_het_2_out %>%
  filter(!grepl("treat:",term)) %>% 
  slice(-c(1:2))

est_ols_het_2_out_gammas <- est_ols_het_2_out %>%
  filter(grepl("treat:",term)) 

table_coefs_alphas <- est_ols_het_2_out_alphas
table_coefs_alphas$term <- c("Intercept", "$D^-$")
table_coefs_betas <- est_ols_het_2_out_betas
table_coefs_betas$term <- c("Age: 31-40","Age: 41-50", "Age: 51+", "Sex: Male", "Education: High school", "Education: College or higher", "Unemployed", "Student", "State employed", "Wealth: Lower middle", "Wealth: Upper middle", "Wealth: High", "Pray: Monthly, Holidays", "Pray: Weekly", "Pray: Everyday", "Kurdish", "Urban", "Financially: Worse", "Financially: Same", "Financially: Better", "Financially: Much better", "Interested in politics: Little", "Interested in politics: Some", "Interested in politics: A lot", "Interested in politics: Very much")  
table_coefs_gammas <- est_ols_het_2_out_gammas
table_coefs_gammas$term <- c("Age: 31-40","Age: 41-50", "Age: 51+", "Sex: Male", "Education: High school", "Education: College or higher", "Unemployed", "Student", "State employed", "Wealth: Lower middle", "Wealth: Upper middle", "Wealth: High", "Pray: Monthly, Holidays", "Pray: Weekly", "Pray: Everyday", "Kurdish", "Urban", "Financially: Worse", "Financially: Same", "Financially: Better", "Financially: Much better", "Interested in politics: Little", "Interested in politics: Some", "Interested in politics: A lot", "Interested in politics: Very much")  

table_stats <- glance(est_ols_het_2)[1,c(6,1)]

table_out_alphas_2 <- list(
  tidy = table_coefs_alphas,
  glance = table_stats)
class(table_out_alphas_2) <- "modelsummary_list"

table_out_betas_2 <- list(
  tidy = table_coefs_betas,
  glance = table_stats)
class(table_out_betas_2) <- "modelsummary_list"

table_out_gammas_2 <- list(
  tidy = table_coefs_gammas,
  glance = table_stats)
class(table_out_gammas_2) <- "modelsummary_list"

# ABSTENTIONS
est_ols_het_0 <- ols_het_ABS
est_ols_het_0_out <- tidy(est_ols_het_0)

est_ols_het_0_out_alphas <- est_ols_het_0_out %>%
  slice(c(1:2))

est_ols_het_0_out_betas <- est_ols_het_0_out %>%
  filter(!grepl("treat:",term)) %>% 
  slice(-c(1:2))

est_ols_het_0_out_gammas <- est_ols_het_0_out %>%
  filter(grepl("treat:",term)) 

table_coefs_alphas <- est_ols_het_0_out_alphas
table_coefs_alphas$term <- c("Intercept", "$D^-$")
table_coefs_betas <- est_ols_het_0_out_betas
table_coefs_betas$term <- c("Age: 31-40","Age: 41-50", "Age: 51+", "Sex: Male", "Education: High school", "Education: College or higher", "Unemployed", "Student", "State employed", "Wealth: Lower middle", "Wealth: Upper middle", "Wealth: High", "Pray: Monthly, Holidays", "Pray: Weekly", "Pray: Everyday", "Kurdish", "Urban", "Financially: Worse", "Financially: Same", "Financially: Better", "Financially: Much better", "Interested in politics: Little", "Interested in politics: Some", "Interested in politics: A lot", "Interested in politics: Very much")  
table_coefs_gammas <- est_ols_het_0_out_gammas
table_coefs_gammas$term <- c("Age: 31-40","Age: 41-50", "Age: 51+", "Sex: Male", "Education: High school", "Education: College or higher", "Unemployed", "Student", "State employed", "Wealth: Lower middle", "Wealth: Upper middle", "Wealth: High", "Pray: Monthly, Holidays", "Pray: Weekly", "Pray: Everyday", "Kurdish", "Urban", "Financially: Worse", "Financially: Same", "Financially: Better", "Financially: Much better", "Interested in politics: Little", "Interested in politics: Some", "Interested in politics: A lot", "Interested in politics: Very much")  

table_stats <- glance(est_ols_het_0)[1,c(6,1)]

table_out_alphas_0 <- list(
  tidy = table_coefs_alphas,
  glance = table_stats)
class(table_out_alphas_0) <- "modelsummary_list"

table_out_betas_0 <- list(
  tidy = table_coefs_betas,
  glance = table_stats)
class(table_out_betas_0) <- "modelsummary_list"

table_out_gammas_0 <- list(
  tidy = table_coefs_gammas,
  glance = table_stats)
class(table_out_gammas_0) <- "modelsummary_list"

# AKP TWO-PARTY VOTE SHARE
est_ols_het_two <- ols_het_AKP_share
est_ols_het_two_out <- tidy(est_ols_het_two)

est_ols_het_two_out_alphas <- est_ols_het_two_out %>%
  slice(c(1:2))

est_ols_het_two_out_betas <- est_ols_het_two_out %>%
  filter(!grepl("treat:",term)) %>% 
  slice(-c(1:2))

est_ols_het_two_out_gammas <- est_ols_het_two_out %>%
  filter(grepl("treat:",term))
est_ols_het_two_out_gammas$term <- est_ols_het_two_out_betas$term


table_coefs_alphas <- est_ols_het_two_out_alphas
table_coefs_alphas$term <- c("Intercept", "$D^-$")
table_coefs_betas <- est_ols_het_two_out_betas
table_coefs_betas$term <- c("Age: 31-40","Age: 41-50", "Age: 51+", "Sex: Male", "Education: High school", "Education: College or higher", "Unemployed", "Student", "State employed", "Wealth: Lower middle", "Wealth: Upper middle", "Wealth: High", "Pray: Monthly, Holidays", "Pray: Weekly", "Pray: Everyday", "Kurdish", "Urban", "Financially: Worse", "Financially: Same", "Financially: Better", "Financially: Much better", "Interested in politics: Little", "Interested in politics: Some", "Interested in politics: A lot", "Interested in politics: Very much")            
table_coefs_gammas <- est_ols_het_two_out_gammas
table_coefs_gammas$term <- c("Age: 31-40","Age: 41-50", "Age: 51+", "Sex: Male", "Education: High school", "Education: College or higher", "Unemployed", "Student", "State employed", "Wealth: Lower middle", "Wealth: Upper middle", "Wealth: High", "Pray: Monthly, Holidays", "Pray: Weekly", "Pray: Everyday", "Kurdish", "Urban", "Financially: Worse", "Financially: Same", "Financially: Better", "Financially: Much better", "Interested in politics: Little", "Interested in politics: Some", "Interested in politics: A lot", "Interested in politics: Very much")            

table_stats <- glance(est_ols_het_two)[1,c(6,1)]

table_out_alphas_two <- list(
  tidy = table_coefs_alphas,
  glance = table_stats)
class(table_out_alphas_two) <- "modelsummary_list"

table_out_betas_two <- list(
  tidy = table_coefs_betas,
  glance = table_stats)
class(table_out_betas_two) <- "modelsummary_list"

table_out_gammas_two <- list(
  tidy = table_coefs_gammas,
  glance = table_stats)
class(table_out_gammas_two) <- "modelsummary_list"

# COMBINE
modelsummary(list(table_out_alphas_1, table_out_betas_1, table_out_gammas_1, table_out_alphas_2, table_out_betas_2, table_out_gammas_2, table_out_alphas_0, table_out_betas_0, table_out_gammas_0, table_out_alphas_two, table_out_betas_two, table_out_gammas_two), 
             stars =  c('*' = .1, '**' = .05,  '***' = .01), 
             escape = FALSE)

# options("modelsummary_format_numeric_latex" = "plain")
# modelsummary(list(table_out_alphas_1, table_out_betas_1, table_out_gammas_1, table_out_alphas_2, table_out_betas_2, table_out_gammas_2, table_out_alphas_0, table_out_betas_0, table_out_gammas_0, table_out_alphas_two, table_out_betas_two, table_out_gammas_two), 
#              stars =  c('*' = .1, '**' = .05,  '***' = .01), 
#              escape = FALSE,
#              output = "paper, table 5.tex")
```

# Figure 7
* The size of each educational subgroup
```{R}
df_het %>% 
  select(district_id, weight, edu_cat) %>%
  filter(!is.na(edu_cat)) %>%
  mutate(edu_temp =model.matrix( ~ edu_cat - 1, data=.)) %>%
  pivot_longer(cols = starts_with("edu_temp"), names_to = "edu_level", values_to = "yes") %>%
  do(lm_robust(yes ~ 1, data = ., clusters=district_id, weight=weight) %>% tidy) %>%
  select(outcome, estimate, conf.low, conf.high) %>%
  mutate(across(estimate:conf.high, ~ .x*100)) %>%
  mutate(across(estimate:conf.high, ~ round(.x, 2)))
```

### Plot
```{R}
df_plot <- df_het %>%
  select(rid, weight, district_id, treat, vote_1, vote_2, vote_0, vote_share=AKP_share, edu_cat) %>%
  pivot_longer(cols = starts_with("vote_"), names_to = "vote", values_to = "choice") %>%
  filter(!is.na(treat), !is.na(vote), !is.na(edu_cat)) %>%
  group_by(edu_cat, treat, vote) %>% 
  do(lm_robust(choice ~ 1, data = ., clusters=district_id, weight=weight) %>% tidy) %>%
  select(edu_cat, treat, vote, estimate, conf.low, conf.high) %>%
  arrange(vote, treat, edu_cat)

plot_xaxis <- c("Highest completed education")
plot_labels <- c("Less than \n high school\n(55.69%)","High \n school\n(25.31%)","College \n or higher\n(18.99%)")
df_plot %>% 
  mutate(treat=as.factor(treat)) %>%
  mutate(vote=recode_factor(vote,
    `vote_0`= "Abstain",
    `vote_1`= "Vote AKP", 
    `vote_2`= "Vote CHP", 
    `vote_share`= "AKP Two Party Vote Share")) %>%
  mutate(vote=factor(vote, levels=c("AKP Two Party Vote Share", "Vote AKP", "Vote CHP", "Abstain"))) %>%
  ggplot(aes(x=edu_cat, y=estimate, col=treat, group = treat)) +
  coord_cartesian(ylim = c(0, 1)) +
  geom_hline(yintercept=0) +
  geom_hline(yintercept=0.5, col="grey50") +
  geom_hline(yintercept=1) +
  geom_line(aes(col=treat, group=treat, linetype = treat),  size=1) + 
  geom_point(aes(col=treat, shape=treat), size=2) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, col=treat, group=treat), width=0.05) +
  scale_shape_manual(name  ="", values=c(17, 16), labels=c("     AKP v. CHP","D- AKP v. CHP")) +
  scale_linetype_manual(name  ="", values=c("solid", "dashed"), labels=c("     AKP v. CHP","D- AKP v. CHP")) +
  scale_color_manual(name  ="", values=c("black", "red"),  labels=c("     AKP v. CHP","D- AKP v. CHP")) +
  scale_x_discrete(name =plot_xaxis, labels=plot_labels) + 
  labs(y = "Fraction") + 
  theme_minimal() +
  facet_wrap(~ vote)

# SAVE THE PLOT
ggsave("figures/figure_7.png", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_7.eps", dpi = 1200, width = 10, height = 6, units = "in")
ggsave("figures/figure_7.pdf", dpi = 1200, width = 10, height = 6, units = "in")
```
